package com.redsoil.house.ui.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.redsoil.house.R;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.model.bean.ProductDetailVO;
import com.redsoil.house.model.http.api.ApiConstants;
import com.redsoil.house.ui.mine.contract.FoodsBuyContract;
import com.redsoil.house.ui.mine.presenter.FoodsBuyPresenter;
import com.redsoil.house.ui.news.activity.NewsInfoActivity;
import com.redsoil.house.ui.news.contract.NewsInfoContract;
import com.redsoil.house.ui.news.presenter.NewsInfoPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;

import butterknife.BindView;
import butterknife.OnClick;

public class FoodsBuyActivity extends RootActivity<FoodsBuyPresenter> implements FoodsBuyContract.View,XBaseTopBarImp{

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.goods_img)
    ImageView goods_img;

    @BindView(R.id.goods_title_tv)
    TextView goods_title_tv;

    @BindView(R.id.reduce_img)
    ImageView reduce_img;

    @BindView(R.id.buy_num_tv)
    TextView buy_num_tv;

    @BindView(R.id.add_img)
    ImageView add_img;

    @BindView(R.id.goods_sum_tv)
    TextView goods_sum_tv;

    @BindView(R.id.receipt_name_edit)
    EditText receipt_name_edit;

    @BindView(R.id.customer_phone_edit)
    EditText customer_phone_edit;

    @BindView(R.id.receipt_address_edit)
    EditText receipt_address_edit;

    @BindView(R.id.express_cost_tv)
    TextView express_cost_tv;

    @BindView(R.id.seller_message_edit)
    EditText seller_message_edit;

    @BindView(R.id.submit_button)
    Button submit_button;

    private ProductDetailVO productDetailVO;

    private int count = 1;

    @Override
    protected int getLayout() {
        return R.layout.activity_foods_buy;
    }

    @Override
    protected void initEventAndData() {
        main_topbar.setxBaseTopBarImp(this);

        productDetailVO = getIntent().getParcelableExtra("data");

        Glide.with(mContext)
                .load(ApiConstants.STATIC_FILE+productDetailVO.getDpic().get(0).getPic())
                .centerCrop()
                .into(goods_img);

        goods_title_tv.setText(productDetailVO.getName());
        calculation();
    }

    private void calculation(){
        buy_num_tv.setText(count+"");
        goods_sum_tv.setText(""+count*productDetailVO.getCurrentPrice());
    }

    @OnClick(R.id.reduce_img)
    void onReduce(){
        if(count <= 1){
            return;
        }
        count--;
        calculation();
    }

    @OnClick(R.id.add_img)
    void onAdd(){
        count++;
        calculation();
    }

    @Override
    public void responeError(String errorMsg) {
        ToastUtils.showLongToast("下单失败，请重试");
    }

    @Override
    public void postOrderSuccess() {
        ToastUtils.showLongToast("下单成功");
        finish();
    }

    @OnClick(R.id.submit_button)
    void onSubmitButton(){

        String phone = customer_phone_edit.getText().toString();
        if (StringUtils.isSpace(phone)) {
            ToastUtils.showShortToast("手机号不能为空");
            return;
        }

        String receivingAddr = receipt_address_edit.getText().toString();
        if (StringUtils.isSpace(receivingAddr)) {
            ToastUtils.showShortToast("收货地址不能为空");
            return;
        }

        String receivingName = receipt_name_edit.getText().toString();
        if (StringUtils.isSpace(receivingName)) {
            ToastUtils.showShortToast("收货人不能为空");
            return;
        }

        mPresenter.postOrder(
                productDetailVO.getId(),
                productDetailVO.getCurrentPrice()+"",
                buy_num_tv.getText().toString(),
                phone,
                receivingAddr,
                receivingName,
                seller_message_edit.getText().toString(),
                "1",
                ""
                );
    }

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {}

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context,ProductDetailVO productDetailVO) {
        Intent intent = new Intent(context, FoodsBuyActivity.class);
        intent.putExtra("data",productDetailVO);
        return intent;
    }

}
